//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package comment_dao

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/project_comment_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type CommentDbItem struct {
	CommentId  string `bson:"_id"`
	ProjectId  string `bson:"projectId"`
	TargetId   string `bson:"targetId"`
	TargetType uint32 `bson:"targetType"`
	SendUserId string `bson:"sendUserId"`
	SendTime   int64  `bson:"sendTime"`
	Content    string `bson:"content"`
}

func (item *CommentDbItem) ToComment() *project_comment_api.Comment {
	return &project_comment_api.Comment{
		CommentId:  item.CommentId,
		SendUserId: item.SendUserId,
		SendTime:   item.SendTime,
		Content:    item.Content,
	}
}

type _CommentDao struct {
	mc *mongo.Collection
}

func newCommentDao(db *mongo.Database) (*_CommentDao, error) {
	mc := db.Collection(db_ns.DB_NS_COMMENT + "comment")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"projectId", 1}, {"targetId", 1}, {"sendTime", -1}},
	})
	if err != nil {
		return nil, err
	}
	return &_CommentDao{mc: mc}, nil
}

func (db *_CommentDao) Insert(ctx context.Context, dbItem *CommentDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_CommentDao) Exist(ctx context.Context, commentId, projectId string, targetType uint32, targetId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{
		"_id":        commentId,
		"projectId":  projectId,
		"targetType": targetType,
		"targetId":   targetId,
	})
	return count > 0, err
}

func (db *_CommentDao) Get(ctx context.Context, commentId, projectId string, targetType uint32, targetId string) (*CommentDbItem, error) {
	retItem := &CommentDbItem{}
	err := db.mc.FindOne(ctx, bson.M{
		"_id":        commentId,
		"projectId":  projectId,
		"targetType": targetType,
		"targetId":   targetId,
	}).Decode(retItem)
	return retItem, err
}

func (db *_CommentDao) Remove(ctx context.Context, commentId, projectId string, targetType uint32, targetId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{
		"_id":        commentId,
		"projectId":  projectId,
		"targetType": targetType,
		"targetId":   targetId,
	})
	return err
}

func (db *_CommentDao) RemoveByTarget(ctx context.Context, projectId string, targetType uint32, targetId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{
		"projectId":  projectId,
		"targetType": targetType,
		"targetId":   targetId,
	})
	return err
}

func (db *_CommentDao) Count(ctx context.Context, projectId string, targetType uint32, targetId string) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{
		"projectId":  projectId,
		"targetType": targetType,
		"targetId":   targetId,
	})
	return uint32(count), err
}

func (db *_CommentDao) List(ctx context.Context, projectId string, targetType uint32, targetId string, offset, limit int64) ([]*CommentDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{
		"projectId":  projectId,
		"targetType": targetType,
		"targetId":   targetId,
	}, options.Find().SetSkip(offset).SetLimit(limit).SetSort(bson.M{"sendTime": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*CommentDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_CommentDao) UpdateContent(ctx context.Context, commentId, projectId string, targetType uint32, targetId, content string) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{
		"_id":        commentId,
		"projectId":  projectId,
		"targetType": targetType,
		"targetId":   targetId,
	}, bson.M{"$set": bson.M{"content": content}})
	return err
}

func (db *_CommentDao) RemoveCicdTarget(ctx context.Context) error {
	// COMMENT_TARGET_CI_CD = 4;         // CI/CD(废弃)
	_, err := db.mc.DeleteMany(ctx, bson.M{
		"targetType": 4,
	})
	return err
}

// 把api coll和dta anno 类型设置为entry
// COMMENT_TARGET_API_COLL = 5;      // API集合
// COMMENT_TARGET_DATA_ANNO = 6;     // 数据标注
func (db *_CommentDao) Upgrade(ctx context.Context) error {
	_, err := db.mc.UpdateMany(ctx, bson.M{"targetType": bson.M{"$in": []uint32{5, 6}}}, bson.M{"$set": bson.M{"targetType": 0}})
	return err
}
